package com.cicadasmall.system.wrapper;

import com.cicadasmall.common.constant.Constant;
import com.cicadasmall.common.func.Fn;
import com.cicadasmall.common.utils.SpringContextUtils;
import com.cicadasmall.common.utils.TreeUtils;
import com.cicadasmall.common.utils.WarpsUtils;
import com.cicadasmall.common.wrapper.EntityWrapper;
import com.cicadasmall.data.domain.DeptDO;
import com.cicadasmall.system.service.IDeptService;
import com.cicadasmall.system.vo.DeptVO;

import java.util.List;


public class DeptWrapper implements EntityWrapper<DeptDO, DeptVO> {

    private static final IDeptService deptService;

    static {
        deptService = SpringContextUtils.getBean(IDeptService.class);
    }

    public static DeptWrapper newBuilder() {
        return new DeptWrapper();
    }

    @Override
    public DeptVO entityVO(DeptDO entity) {
        DeptVO deptVo = WarpsUtils.copyTo(entity, DeptVO.class);
        assert deptVo != null;
        if (Fn.isNotNull(deptVo.getParentId()) && Fn.notEqual(Constant.PARENT_ID, deptVo.getParentId())) {
            DeptDO parentDept = deptService.getById(deptVo.getParentId());
            deptVo.setParentName(parentDept.getDeptName());
        } else {
            deptVo.setParentName("顶级部门");
        }
        return deptVo;
    }

    @Override
    public List<DeptVO> treeVO(List<DeptDO> list) {
        return TreeUtils.toTree(this.listVO(list));
    }

}
